package com.bjc.platform.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.bjc.platform.dao.GenericDao;
import com.bjc.platform.service.BaseService;

/**
 * Base class for all Manager implements.
 * @author cj
 *
 */
public class BaseServiceImpl implements BaseService {
    /**
     * Log variable for all child classes.
     */
    protected final Log log = LogFactory.getLog(getClass());
    
    @Autowired
	protected GenericDao genericDao;
    
	@Override
	public boolean checkValueOfEntity(String table, String column,
			String value, int id,String condition) {
		StringBuilder builder=new StringBuilder("select t.id count from ");
		if(id==0){
			builder.append(table+" t ");
			builder.append("where t."+column+"='"+value+"' ");
		}else{
			builder.append(table+" t ");
			builder.append("where t.id<>"+id+" and t."+column+"='"+value+"' ");
		}
		if(condition!=null&&condition.length()>0){
			builder.append(condition);
		}
		long count= this.genericDao.countBySql(builder.toString(), new String[]{});
		if(count>0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean checkCascadeOfEntity(String table, String column, int id) {
		StringBuilder builder=new StringBuilder("select t.id itemId from ");

		builder.append(table+" t ");
		builder.append("where t."+column+"="+id+" ");

		long count= this.genericDao.countBySql(builder.toString(), new String[]{});
		if(count>0){
			return true;
		}else{
			return false;
		}
	}
}
